package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.common.constant.RedisConst;
import com.twetec.cms.common.util.cache.RedisCache;
import com.twetec.cms.dao.mapper.system.CmsRoleMenuMapper;
import com.twetec.cms.model.po.system.CmsRoleMenuPO;
import com.twetec.cms.service.system.CmsRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author wugaojian
 * @date 2022/2/9 11:43
 */
@Service
public class CmsRoleMenuServiceImpl extends ServiceImpl<CmsRoleMenuMapper, CmsRoleMenuPO> implements CmsRoleMenuService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean deleteUserPermission() {
        String key = RedisConst.REDISKEY_PERMISSION_MENU + "*";
        return redisCache.deletePattern(key);
    }

    @Override
    public boolean addRoleMenuRelative(String roleIds, ArrayList<Long> menuIdList) {

        ArrayList<CmsRoleMenuPO> cmsRoleMenuPOS = new ArrayList<>();

        // 构建角色和权限菜单的关联对象
        String[] roleIdArray = roleIds.split(",");
        for (String roleId : roleIdArray) {
            for (Long menuId : menuIdList) {
                CmsRoleMenuPO cmsRoleMenuPO = new CmsRoleMenuPO();
                long id = IdWorker.getId();
                cmsRoleMenuPO.setId(id);
                cmsRoleMenuPO.setMenuId(menuId);
                cmsRoleMenuPO.setRoleId(Long.valueOf(roleId));
                cmsRoleMenuPOS.add(cmsRoleMenuPO);
            }
        }

        return this.saveBatch(cmsRoleMenuPOS);
    }
}
